﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Core;
using GraphControls;
using GraphControls.Views;


namespace GraphPlusEditor
{
    public partial class MainForm : Form
    {
        private bool _isselected = false;
        private string buf = "";
        private bool _isErrorText = false;
        private GraphControl _currentControl;
        private List<GraphControl> _moduls;

        public MainForm()
        {
            //projectsControl1 = new ProjectsControl(ref tabControl1);
            _moduls = new List<GraphControl>();
            InitializeComponent();
            projectsControl1.SetMod(_moduls);
            projectsControl1.ProjectOpenEvent += ProjectOpen;
        }

        private void ProjectOpen(Object sender, EventArgs eventArgs)
        {
            AddButton.Enabled = true;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            _currentControl.CurrentItem = 2;
            toolStripButton1.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripButton1.Checked = true;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            _currentControl.CurrentItem = 0;
            toolStripButton2.Checked = false;
            toolStripButton4.Checked = false;
            toolStripButton5.Checked = false;
            toolStripButton3.Checked = false;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            _currentControl.CurrentItem = 1;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton5.Checked = false;
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            _currentControl.CurrentItem = 3;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton3.Checked = false;
            toolStripButton4.Checked = false;
        }


        private void создатьToolStripButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Вы действительно хотите создать новую модель", "Сообщение",
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                _currentControl.NewDoc();
            }
        }



        private void сохранитьToolStripButton_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                projectsControl1.SaveToXML(saveFileDialog1.FileName);
            }
        }

        private void открытьToolStripButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    projectsControl1.OpenXML(openFileDialog1.FileName);
                    toolStripButton1.Enabled = true;
                    toolStripButton2.Enabled = true;
                    toolStripButton3.Enabled = true;
                    toolStripButton4.Enabled = true;
                    toolStripButton5.Enabled = true;
                }
                catch (Exception)
                {
                    MessageBox.Show(this, "Файл поврежден", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }




        private void процессToolStripMenuItem_Click(object sender, EventArgs e)
        {

            NewSchem newSchem = new NewSchem(projectsControl1, Graph.TypeOfGraph.Process);
            newSchem.Closed += AddProcess;
            newSchem.Show(this);
        }

        private void AddProcess(object sender, EventArgs eventArgs)
        {
            if (projectsControl1.IsAdded)
            {
                toolStripButton1.Enabled = true;
                toolStripButton2.Enabled = true;
                toolStripButton3.Enabled = true;
                toolStripButton4.Enabled = true;
                toolStripButton5.Enabled = true;
                GraphControl graphControl = new GraphControl(projectsControl1);
                _moduls.Add(graphControl);
                tabControl1.TabPages.Add(projectsControl1.GetActive().Name);
                graphControl.Parent = tabControl1.TabPages[tabControl1.TabPages.Count - 1];
                graphControl.Structure = projectsControl1.GetActive();
                graphControl.Dock = DockStyle.Fill;
                _currentControl = graphControl;
                projectsControl1.Invalidate();
            }
        }

        private void AddChenal(object sender, EventArgs eventArgs)
        {
            if (projectsControl1.IsAdded)
            {
                GraphControl graphControl = new GraphControl(projectsControl1);
                _moduls.Add(graphControl);
                tabControl1.TabPages.Add(projectsControl1.GetActive().Name);
                graphControl.Parent = tabControl1.TabPages[tabControl1.TabPages.Count - 1];
                graphControl.Structure = projectsControl1.GetActive();
                graphControl.Dock = DockStyle.Fill;
                _currentControl = graphControl;
                projectsControl1.Invalidate();
            }
        }

        private void tabControl1_TabIndexChanged(object sender, EventArgs e)
        {
            //var ActivProgect = _projects.Where(x => x.IsActive == true).ToArray()[0];
            //_currentControl = _moduls.Where( x => x.Structure == ActivProgect.)

        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(projectsControl1.IsProjectOpened)
                _currentControl = _moduls.Where(x => x.Structure.Name == tabControl1.SelectedTab.Text).ToArray()[0];
        }

        private void каанлToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewSchem newSchem = new NewSchem(projectsControl1, Graph.TypeOfGraph.Chenal);
            newSchem.Closed += AddProcess;
            newSchem.Show(this);

        }

        private void toolStripButton3_Click_1(object sender, EventArgs e)
        {
            _currentControl.CurrentItem = 4;
            toolStripButton1.Checked = false;
            toolStripButton2.Checked = false;
            toolStripButton5.Checked = false;
            toolStripButton4.Checked = false;
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                projectsControl1.SaveToXML(saveFileDialog1.FileName);
            }
        }

        private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    projectsControl1.OpenXML(openFileDialog1.FileName);
                    toolStripButton1.Enabled = true;
                    toolStripButton2.Enabled = true;
                    toolStripButton3.Enabled = true;
                    toolStripButton4.Enabled = true;
                    toolStripButton5.Enabled = true;
                }
                catch (Exception)
                {
                    MessageBox.Show(this, "Файл поврежден", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public void LoadFile(string filename)
        {
            projectsControl1.OpenXML(filename);
        }

        private void NewProjectMenuItem_Click(object sender, EventArgs e)
        {
            NewProject newProject = new NewProject(projectsControl1);
            if (newProject.ShowDialog() == DialogResult.OK)
                AddButton.Enabled = true;

        }

        private void MainForm_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ? DragDropEffects.Copy : DragDropEffects.None;
        }

        private void MainForm_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                Object o = e.Data.GetData(DataFormats.FileDrop);
                String[] file = (String[])o;
                XmlDocument document = new XmlDocument();
                document.LoadXml(System.IO.File.ReadAllText(file[0]));
                projectsControl1.OpenXML(document);
            }
            catch (Exception)
            {

                throw;
            }
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
    }
}
